package com.ccc.boot.util;

import com.ccc.boot.log.IpInfo;
import com.ccc.boot.log.LogInfo;
import eu.bitwalker.useragentutils.Browser;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 日志记录工具类
 *
 * @author superC
 * @version 0.1.0
 * @date 2022/4/9
 */

public class LogUtils {

    /**
     * 获取登录日志相关信息
     *
     * @param request  :
     * @param username :
     * @param clientId :
     * @param token
     * @return {@link LogInfo}
     * @date 2022/4/9 20:16
     * @author superC
     */
    public static LogInfo getLoginLog(HttpServletRequest request, String username, Integer clientId, String token) {
        LogInfo logInfo = getLogInfo(request, username, clientId);

        logInfo.setTokenkey(token);

        return logInfo;
    }


    /**
     * 获取操作日志相关信息
     *
     * @param request
     * @param username
     * @return {@link LogInfo}
     * @date 2022/5/1 19:51
     * @author superC
     */

    public static LogInfo getOperateLog(HttpServletRequest request, String username) {
        LogInfo logInfo = getLogInfo(request, username, null);

        // 获取请求得路径和方法名
        String requestUri = request.getRequestURI();
        logInfo.setPath(requestUri);
        String method = request.getMethod();
        logInfo.setMethod(method);
        return logInfo;
    }

    /**
     * 获取异常日志相关信息
     *
     * @param request
     * @param code
     * @param message
     * @param errorMessage
     * @return {@link LogInfo}
     * @date 2022/5/1 19:51
     * @author superC
     */

    public static LogInfo getErrorLog(HttpServletRequest request, String code, String message, String errorMessage) {
        LogInfo logInfo = getLogInfo(request, null, null);

        // 获取请求得路径和方法名
        String requestUri = request.getRequestURI();
        logInfo.setPath(requestUri);
        String method = request.getMethod();
        logInfo.setMethod(method);

        // 异常信息
        logInfo.setCode(code);
        logInfo.setMessage(message);
        logInfo.setErrorMessage(errorMessage);

        return logInfo;
    }

    /**
     * 获取日志信息
     *
     * @param request  请求request
     * @param username 登录账户
     * @param clientId 客户端ID：1-WEB端；2-APP端；
     * @return {@link LogInfo}
     * @date 2022/5/2 11:25
     * @author superC
     */

    public static LogInfo getLogInfo(HttpServletRequest request, String username, Integer clientId) {
        LogInfo logInfo = new LogInfo();
        if (clientId != null) {
            logInfo.setClientType(clientId);
        }
        logInfo.setUsername(username);

        // IP
        String ip = UserAgentUtils.getIpAddr(request);
        logInfo.setIp(ip);

        // 地址
        IpInfo ipInfo = UserAgentUtils.getIpInfo(ip);
        logInfo.setCountry(ipInfo.getCountry());
        logInfo.setRegion(ipInfo.getRegion());
        logInfo.setCity(ipInfo.getCity());

        // 运营商
        logInfo.setIsp(ipInfo.getIsp());

        // 用户代理
        Browser browser = UserAgentUtils.getBrowser(request);
        if (browser != null) {
            logInfo.setUserAgent(browser.getName());
        }
        return logInfo;
    }

    public static String getAddress(LogInfo info) {
        String address = "";
        if (StringUtils.isNotEmpty(info.getCountry())) {
            address = info.getCountry();
        }
        if (StringUtils.isNotEmpty(info.getRegion())) {
            address += info.getRegion();
        }
        if (StringUtils.isNotEmpty(info.getCity())) {
            address += info.getCity();
        }
        return address;
    }

}
